package com.nolydake.swingnote.gui.ext.fordertree;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import org.apache.log4j.Logger;

import com.nolydake.swingnote.model.NoteForder;
import com.nolydake.swingnote.service.note.NoteServiceFactory;

public class ForderTree extends JTree {

    /** 日志 */
    private static final Logger LOG = Logger.getLogger(ForderTree.class);

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -2260060717247543178L;

    public ForderTree() {
        super();
    }

    /**
     * 默认的构造方法
     * 
     * @param forderTreeRootNode
     */
    public ForderTree(DefaultMutableTreeNode forderTreeRootNode) {
        super(forderTreeRootNode);
        initGui();
    }

    private ForderTreePopup pop = null;

    private void initGui() {
        // 我们使用自定义的鼠标事件，来弹出菜单。这样我们可以灵活的控制选中事件
        // this.setComponentPopupMenu(new ForderTreePopup(this));
        pop = new ForderTreePopup(this);

        this.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                // 不管是左键还是右键，我们都选中当前鼠标所在的位置对应的菜单
                TreePath path = ForderTree.this.getPathForLocation(e.getX(), e.getY());

                if (path == null) {
                    return;
                }

                ForderTree.this.setSelectionPath(path);

                LOG.debug(path);

                if (e.getButton() == MouseEvent.BUTTON3) {
                    pop.show(ForderTree.this, e.getX(), e.getY());
                }
            }
        });
    }

    public void addForder() {
        TreePath path = this.getSelectionPath();

        DefaultMutableTreeNode node1 = (DefaultMutableTreeNode) path.getLastPathComponent();
        NoteForder forder = (NoteForder) node1.getUserObject();

        if (forder != null) {
            // forder.id + "--" + forder.name
            NoteForder newForder = NoteServiceFactory.getService().addForder(forder.id);

            DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newForder);
            node1.add(newNode);

            DefaultTreeModel model = (DefaultTreeModel) this.getModel();
            // model.insertNodeInto(newNode, node1, index);

            TreePath newPath = new TreePath(model.getPathToRoot(newNode));
            this.scrollPathToVisible(newPath);

            this.setSelectionPath(newPath);

            this.startEditingAtPath(newPath);
            this.updateUI();

        } else {
            JOptionPane.showMessageDialog(null, "你什么都没有选中，应该还不能创建文件夹");
        }
    }

    public void editSelectNode() {

    }

    public void delForder() {

        TreePath path = this.getSelectionPath();

        if (path == null) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("没有在树上找到要处理的节点");
            }

            return;
        }

        DefaultMutableTreeNode selectNote = (DefaultMutableTreeNode) path.getLastPathComponent();
        NoteForder forder = (NoteForder) selectNote.getUserObject();

        if (forder == null) {

            if (LOG.isDebugEnabled()) {
                LOG.debug("没有在树上找到要处理的目录");
            }

            return;
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("删除：" + forder.id + "-----" + forder.name);
        }
        
        
        
        
        String message = NoteServiceFactory.getService().deleteNoteForder(forder.id);

        if (message != null) {
            JOptionPane.showMessageDialog(this, message);
        }

        // 从画面的树里面移除
        this.setSelectionPath(path.getParentPath());

        DefaultTreeModel model = (DefaultTreeModel) this.getModel();
        model.removeNodeFromParent(selectNote);
    }
}
